/*
 *所有关于PROC_PROGRAM类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*PROC_PROGRAMService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/

using System;
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.System.IRepositories;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using VOL.Core.ManageUser;
using VOL.Entity.VM;
using StackExchange.Redis;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace VOL.System.Services
{
    public partial class PROC_PROGRAMService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IPROC_PROGRAMRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public PROC_PROGRAMService(
            IPROC_PROGRAMRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            var oldDetailData = saveDataModel.MainData;
            Dictionary<string, object> itemnew0 = oldDetailData;
            foreach (var item1 in itemnew0)
            {
                if (item1.Key == "ParentId")
                {
                    string a = item1.Value.ToString();
                    if (a == "")
                    {
                        oldDetailData["ParentId"] = "0";
                    }
                    else
                    {
                        oldDetailData["ParentId"] = item1.Value;
                    }
                   
                }
            }
            WebResponseContent rs = new WebResponseContent();
          //  var rs=base.Add(saveDataModel);
            var order = oldDetailData["Identifier"];
       var sequence= int.Parse(oldDetailData["sequence"].ToString());
       PROC_PROGRAM itempProgram= _repository.DbContext.Set<PROC_PROGRAM>().Where(t => t.Identifier == order && t.sequence == sequence && t.tool_name != oldDetailData["program_name"].ToString()).FirstOrDefault();
            EQU_TOOL itemtTool = _repository.DbContext.Set<EQU_TOOL>().Where(t => t.Identifier == order && t.program_seq == sequence).FirstOrDefault();
            var secornSeq = _repository.DbContext.Set<EQU_TOOL>()
                .Where(t => t.Identifier == order && t.program_seq == sequence).Count() + 1;
            using (var transaction = _repository.DbContext.Database.BeginTransaction())
            {
                try
                {
                        EQU_TOOL eQU_TOOL = new EQU_TOOL();
                        //eQU_TOOL.work_order_id = workOrderId;
                        //var iNTE_SHELVESFirst = iNTE_SHELVES.Where(t => t.DBCode == item.location).FirstOrDefault();
                        //if (iNTE_SHELVESFirst != null)
                        //{
                        //    eQU_TOOL.SHELVES_Id = iNTE_SHELVESFirst.INTE_SHELVES_Id.ToString().ToUpper();
                        //    eQU_TOOL.location = iNTE_SHELVESFirst.DBCode;
                        //}

                        eQU_TOOL.tool_name = oldDetailData["tool_name"].ToString();// itemtTool.tool_name;

                        eQU_TOOL.SecornSeq=secornSeq;
                        eQU_TOOL.Identifier =order.ToString();
                        eQU_TOOL.knife_length = itemtTool.knife_length;
                        eQU_TOOL.blade_length = itemtTool.blade_length;
                        eQU_TOOL.category = itemtTool.category;
                        eQU_TOOL.handle_type = itemtTool.handle_type;
                        eQU_TOOL.collet_type = itemtTool.collet_type;
                       // eQU_TOOL.reality_knife_width = item.knife_width;
                        eQU_TOOL.reality_pairing_length = itemtTool.knife_length;
                        eQU_TOOL.reality_R = 0;
                        eQU_TOOL.vendor_code = itemtTool.vendor_code;
                        eQU_TOOL.tool_materials = itemtTool.tool_materials;
                        eQU_TOOL.knife_width = itemtTool.knife_width ;
                        eQU_TOOL.R = itemtTool.R;
                        eQU_TOOL.time = itemtTool.time;
                        eQU_TOOL.accuracy = itemtTool.accuracy;
                        eQU_TOOL.program_seq = itempProgram.sequence;
                    //eQU_TOOL.work_order_status = work_Order.process_Status;
                    eQU_TOOL.CreateBy = UserContext.Current.UserName;
                        eQU_TOOL.CreateOn = DateTime.Now;
                        //eQU_TOOL.Identifier = proCodeMain;
                        //eQU_TOOL.Ace1LTheory = item.Ace1LTheory;
                        //eQU_TOOL.Ace1DTheory = item.Ace1DTheory;
                        //eQU_TOOL.Ace2LTheory = item.Ace2LTheory;
                        //eQU_TOOL.Ace2DTheory = item.Ace2DTheory;
                        //eQU_TOOL.Ace3LTheory = item.Ace3LTheory;
                        //eQU_TOOL.Ace3DTheory = item.Ace3DTheory;
                        //eQU_TOOL.Holder_Code = item.Holder_Code;
                        _repository.DbContext.Entry(eQU_TOOL).State = EntityState.Added;

                        PROC_PROGRAM pROC_PROGRAM = new PROC_PROGRAM();
                        pROC_PROGRAM.SecornSeq = secornSeq;
                    pROC_PROGRAM.ParentId = "0";
                    pROC_PROGRAM.UseState = "未使用"; // "1:未使用 2:使用中 3:已使用"
                                                   //pROC_PROGRAM.ftpmachiningcenter_id = itempProgram.ftpmachiningcenter_id;
                    pROC_PROGRAM.sequence = itempProgram.sequence;// int.Parse(itempProgram.sequence);
                    pROC_PROGRAM.program_name = oldDetailData["program_name"].ToString();//itempProgram.program_name;
                    pROC_PROGRAM.Identifier = order.ToString();
                    pROC_PROGRAM.tool_name = eQU_TOOL.tool_name;
                    //pROC_PROGRAM.tool_handle = itempProgram.tool_handle;
                    //pROC_PROGRAM.tool_collet = itempProgram.tool_collet;
                    //pROC_PROGRAM.knife_length = itempProgram.knife_length;
                    //pROC_PROGRAM.blade_length = itempProgram.blade_length;
                    //pROC_PROGRAM.category = itempProgram.category;
                    //pROC_PROGRAM.accuracy = itempProgram.accuracy;
                    //pROC_PROGRAM.rotate_speed = itempProgram.rotate_speed;
                    //pROC_PROGRAM.feed = itempProgram.feed;
                    //pROC_PROGRAM.side_bottom_margin = itempProgram.side_bottom_margin;
                    //pROC_PROGRAM.z_depth = itempProgram.z_depth;
                    //pROC_PROGRAM.tool_materials = itempProgram.tool_materials;
                    //pROC_PROGRAM.vendor_code = itempProgram.vendor_code;
                    //pROC_PROGRAM.remark = itempProgram.remark;
                    //pROC_PROGRAM.time = itempProgram.time;
                    pROC_PROGRAM.CreateBy = UserContext.Current.UserName;
                    pROC_PROGRAM.CreateOn = DateTime.Now;
                    pROC_PROGRAM.ProCode = itempProgram.ProCode;
                    pROC_PROGRAM.Id = Guid.Parse(Guid.NewGuid().ToString().ToUpper());

                    _repository.DbContext.Entry(pROC_PROGRAM).State = EntityState.Added;

                    // }

                    _repository.SaveChanges();
                    transaction.Commit();

                  //  return 1;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                 //   return -1;
                }
            }

            rs.Code = "311";
            rs.Message = "添加成功";
    
            return rs.OK();
        }

        public override WebResponseContent Update(SaveModel saveModel)
        {
            var oldDetailData = saveModel.MainData;
            Dictionary<string, object> itemnew0 = oldDetailData;
            foreach (var item1 in itemnew0)
            {
                if (item1.Key == "ParentId")
                {
                    string a = item1.Value.ToString();
                    if (a == "")
                    {
                        oldDetailData["ParentId"] = "0";
                    }
                    else
                    {
                        oldDetailData["ParentId"] = item1.Value;
                    }

                }
            }

   
            return base.Update(saveModel);
        }

        public override WebResponseContent Del(object[] keys, bool delList = true)
        {
            WebResponseContent rs = new WebResponseContent();
            using (var transaction = _repository.DbContext.Database.BeginTransaction())
            {
                try
                {
                    for (int i = 0; i < keys.Length; i++)
                    {

                        PROC_PROGRAM pROC_PROGRAM = _repository.DbContext.Set<PROC_PROGRAM>().Where(t => t.Id == Guid.Parse(keys[i].ToString().ToUpper())
                        ).FirstOrDefault();
                        if (pROC_PROGRAM != null)
                        {
                            _repository.DbContext.Entry(pROC_PROGRAM).State = EntityState.Deleted;
                        }

                        EQU_TOOL eQU_TOOL = _repository.DbContext.Set<EQU_TOOL>().Where(t => t.Identifier == pROC_PROGRAM.Identifier
                        &&t.program_seq== pROC_PROGRAM.sequence
                        &&t.tool_name== pROC_PROGRAM.tool_name).FirstOrDefault();

                        if (eQU_TOOL != null)
                        {
                            _repository.DbContext.Entry(eQU_TOOL).State = EntityState.Deleted;
                        }

                       


                        _repository.SaveChanges();
                    }
                   
                    transaction.Commit();

                    //  return 1;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    //   return -1;
                }
            }
            rs.Message= "删除成功";
            return rs.OK();
            //return base.Del(keys, delList);
        }
    }
}
